// 表单验证
var username = document.querySelector("[name = 'username']");
var password = document.querySelector("[name = 'password']");
var repass = document.querySelector("[name = 'repass']");
var tel = document.querySelector("[name = 'tel']");
var email = document.querySelector("[name = 'email']");
var agree = document.querySelector("[name = 'protocol']")
var form = document.querySelector("form");
var p = document.createElement("p");
p.className = "error";
username.onfocus = function () {
    if (this.nextElementSibling) { // 如果存在错误信息的p,就删除
        this.parentElement.removeChild(this.nextElementSibling);
    }
}
password.onfocus = function () {
    if (this.nextElementSibling) { // 如果存在错误信息的p,就删除
        this.parentElement.removeChild(this.nextElementSibling);
    }
}
repass.onfocus = function () {
    if (this.nextElementSibling) { // 如果存在错误信息的p,就删除
        this.parentElement.removeChild(this.nextElementSibling);
    }
}
tel.onfocus = function () {
    if (this.nextElementSibling) { // 如果存在错误信息的p,就删除
        this.parentElement.removeChild(this.nextElementSibling);
    }
}
email.onfocus = function () {
    if (this.nextElementSibling) { // 如果存在错误信息的p,就删除
        this.parentElement.removeChild(this.nextElementSibling);
    }
}
agree.onchange = function () {
    if (this.nextElementSibling) {
        if (this.checked) {
            this.parentElement.removeChild(this.nextElementSibling);
        }
    }
}

form.onsubmit = function (e) {
    // 取消默认事件
    var e = e || window.event;
    if (window.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
    if (username.value == "") {
        p.innerHTML = "用户名不能为空";
        username.parentElement.appendChild(p);
        return false;
    }
    var nameReg = /^[a-zA-Z]\w{5,15}$/;
    if (!nameReg.test(username.value)) {
        p.innerHTML = "用户名必须字母开头，由字母数字组成，6-16位";
        username.parentElement.appendChild(p);
        return false;
    }
    if (password.value == "") {
        p.innerHTML = "密码不能为空";
        password.parentElement.appendChild(p);
        return false;
    }
    var psdReg = /^\w{6,16}$/;
    if (!psdReg.test(password.value)) {
        p.innerHTML = "密码有数字字母组成，6-16位";
        password.parentElement.appendChild(p);
        return false;
    }
    if (password.value != repass.value) {
        p.innerHTML = "两次密码不一致";
        repass.parentElement.appendChild(p);
        return false;
    }

    if (tel.value == "") {
        p.innerHTML = "手机号不能为空";
        tel.parentElement.appendChild(p);
        return false;
    }
    var telReg = /^1[3-9]\d{9}$/;
    if (!telReg.test(tel.value)) {
        p.innerHTML = "请输入正确的手机号";
        tel.parentElement.appendChild(p);
        return false;
    }
    if (email.value == "") {
        p.innerHTML = "邮箱不能为空";
        email.parentElement.appendChild(p);
        return false;
    }
    var emailReg = /^[a-zA-Z0-9]\w+@[a-zA-Z0-9]{2,3}\.(com|cn)$/;
    if (!emailReg.test(email.value)) {
        p.innerHTML = "请输入正确的邮箱";
        email.parentElement.appendChild(p);
        return false;
    }
    if (!agree.checked) {
        p.innerHTML = "请同意注册协议";
        agree.parentElement.appendChild(p);
        return false;
    }

    // 发送注册请求
    pAjax({
        url: "./serve/register.php",
        data: {
            username: username.value,
            password: password.value,
            tel: tel.value,
            email: email.value
        },
        method: "post"
    }).then(res => {
        // console.log(res);
        var { meta: { status, msg } } = res;
        if (status == 102) {
            alert(msg)
            location.href = "login.html";
        } else {
            alert(msg);
            return false;
        }
    })
}